package cn.smart.wapperx.advice;

import cn.hutool.json.JSONUtil;
import cn.smart.core.annotation.NoWapperX;
import cn.smart.core.dto.ApiResponse;
import cn.smart.core.exception.BizException;
import com.github.pagehelper.Page;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;
import java.util.List;

@RestControllerAdvice
public class ApiResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        Class<?> declaringClass = methodParameter.getDeclaringClass();
        //判断类上是否有NoWapper注解,如果有，则不包装
        if (declaringClass.isAnnotationPresent(NoWapperX.class)) {
            return false;
        }
        //判断方法上是否有NoWapper注解,如果有，则不包装
        if (methodParameter.hasMethodAnnotation(NoWapperX.class)) {
            return false;
        }
        return true;
    }
    @ExceptionHandler(Exception.class)
    // 处理Exception类型的异常
    public ApiResponse<Object> handleException(Exception e) {
        e.printStackTrace();
        // 打印异常堆栈信息
        if (e instanceof BizException){
            // 如果异常是BizException类型
            BizException bizException = (BizException) e;
            // 将异常转换为BizException类型
            return ApiResponse.error(bizException.getCode(), bizException.getMessage());
            // 返回错误信息，包括错误码和错误信息
        }
        return ApiResponse.error(999, e.getMessage());
        // 如果异常不是BizException类型，返回错误码999和错误信息"未知异常"
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        //如果是ApiResponse类型，直接返回
        if(body instanceof ApiResponse){
            return body;
        }
        ApiResponse<Object> apiResponse = ApiResponse.success(body); // 这里可以根据业务逻辑返回不同的状态码和消息

        //如果返回类性是String类型，把ApiResponse转换成String类型
        if(aClass == StringHttpMessageConverter.class){
            return JSONUtil.toJsonStr(apiResponse);
        }

/*        if (body instanceof PageInfo){
            PageInfo<?> pageInfo = (PageInfo<?>) body;
            Map<String,Object> data = new HashMap<>();
            data.put("list",pageInfo.getList());
            data.put("total",pageInfo.getTotal());
            data.put("pageNum",pageInfo.getPageNum());
            data.put("pageSize",pageInfo.getPageSize());
            ApiResponse<Map<String,Object>> apiResponse1 = ApiResponse.success(data);
            return apiResponse1;
        }*/
        if (body instanceof Page){
            Page page = (Page) body;
            long total = page.getTotal();
            List items = page.getResult();
            //System.out.println(items);
            HashMap map = new HashMap();
            map.put("total",total);
            map.put("items",items);
            //System.out.println(ApiResponse.success(map));
            return  ApiResponse.success(map);
        }
        return apiResponse;
    }
}
